// 配置来源——系统初始化
const CONFIG_SOURCE_SYSTEM = 1;
// 配置来源——用户自建
const CONFIG_SOURCE_USER = 2;

const columnRender = (h, getField) => {
  return h(
    "div",
    {
      attrs: {
        class: "ivu-table-cell ivu-table-cell-ellipsis",
        title: getField()
      }
    },
    getField()
  );
};

// 主column
export function systemManageColumn() {
  const COLUMN_WIDTH = window.RPConfig.COLUMN_WIDTH
  return [
    {
      title: this.$t("module.configManage.parameterCode"),
      key: "code",
      sortable: "custom",
      minWidth: 120,
      render: (h, params) => {
        return columnRender(h, () => params.row.code);
      }
    },
    {
      title: this.$t("module.configManage.parameterName"),
      key: "name",
      minWidth: 120,
      sortable: "custom",
      render: (h, params) => {
        return columnRender(h, () => params.row.name);
      }
    },
    {
      title: this.$t("module.configManage.sourceValue"),
      minWidth: 200,
      key: "valueSetName",
      render: (h, params) => {
        return columnRender(h, () => (params.row.valueSetName ? params.row.valueSetName + ' (' + params.row.valueSetCode + ')' : ''));
      }
    },
    {
      title: this.$t("module.configManage.application"),
      minWidth: 120,
      key: "appName",
      render: (h, params) => {
        return columnRender(h, () => params.row.appName);
      }
    },
    {
      title: this.$t("module.configManage.tenantName"),
      minWidth: 120,
      key: "tenantName",
      render: (h, params) => {
        return columnRender(h, () => params.row.tenantName);
      }
    },
    {
      title: this.$t("module.configManage.description"),
      key: "description",
      minWidth: 300,
      render: (h, params) => {
        return columnRender(h, () => params.row.description);
      }
    },
    {
      title: this.$t("common.status"),
      key: "enabled",
      align: "center",
      minWidth: 80,
      maxWidth: 80 + COLUMN_WIDTH.MAX_WIDTH_MORE,

      render: (h, params) => {
        return h(
          "StatusButton",
          {
            // 修改StatusButton内容居中
            style: {
              "justify-content": "center"
            },

            props: {
              status: params.row.enabled == 1 ? "0" : "1"
            }
          },
          params.row.enabled == 1 ? this.$t("common.enable") : this.$t("common.disable")
        );
      }
    },
    {
      title: this.$t("common.operating"),
      align: "left",
      fixed: "right",
      width: 120,
      render: (h, params) => {
        // 是否导入的配置项，系统初始化时通过脚本导入
        let isImport = params.row.source == CONFIG_SOURCE_SYSTEM;
        let operations = [
          {
            type: "icon",
            icon: "md-create",
            text: this.$t("common.edit"),
            hidden: isImport || (params.row.tenantCode == window.RPConfig.rootTenantCode ? !this.isRIPPLES : false),
            handle: () => {
              this.onEdit(params.row);
            }
          },
          {
            type: "icon",
            icon: "ios-settings",
            text: this.$t("module.configManage.valueMaintenance"),
            handle: () => this.onSetting(params.row)
          }
        ];
        if (!isImport && params.row.tenantCode == this.tenantCode) {
          operations.push({
            type: "icon",
            icon: "ios-trash",
            text: this.$t("common.delete"),
            hidden: params.row.enabled == 1,
            handle: () => this.onDelete(params.row.id)
          });
        }
        return h("RowTools", {
          props: {
            data: operations
          }
        });
      }
    }
  ];
}

// item column
export function configItemColumn() {
  const getOwnerName = owner => {
    if (!this.owners || !this.owners.length) return owner;
    let ownerInfo = this.owners.filter(item => item.vsiKey === owner);
    ownerInfo = ownerInfo[0] || {};
    return ownerInfo.vsiValue || "";
  };
  const COLUMN_WIDTH = window.RPConfig.COLUMN_WIDTH
  return [
    {
      title: this.$t("module.configManage.configHierarchy"),
      key: "scopeLevel",
      sortable: "custom",
      minWidth: COLUMN_WIDTH.COLUMN_WIDTH_4_WORDS,
      maxWidth: COLUMN_WIDTH.COLUMN_WIDTH_4_WORDS + COLUMN_WIDTH.MAX_WIDTH_MORE,
      render: (h, params) => {
        return columnRender(h, () =>
          this.getLevelName(
            params.row.scopeLevel == 0 ? this.$t("module.configManage.platformDefault") : params.row.scopeLevel
          )
        );
      }
    },
    {
      title: this.$t("module.configManage.hierarchyValue"),
      key: "scopeContextName",
      sortable: "custom",
      align: "center",
      minWidth: COLUMN_WIDTH.COLUMN_WIDTH_3_WORDS,
      maxWidth: COLUMN_WIDTH.COLUMN_WIDTH_3_WORDS + COLUMN_WIDTH.MAX_WIDTH_MORE,
      render: (h, params) => {
        return columnRender(h, () => params.row.scopeContextName);
      }
    },
    {
      title: this.$t("module.configManage.parameter"),
      key: "value",
      render: (h, params) => {
        return columnRender(h, () => params.row.value);
      }
    },
    {
      title: this.$t("module.configManage.description"),
      key: "description",
      render: (h, params) => {
        return columnRender(h, () => params.row.description);
      }
    },
    {
      title: this.$t("common.status"),
      key: "enabled",
      align: "center",
      minWidth: 80,
      maxWidth: 80 + COLUMN_WIDTH.MAX_WIDTH_MORE,

      render: (h, params) => {
        return h(
          "StatusButton",
          {
            // 修改StatusButton内容居中
            style: {
              "justify-content": "center"
            },

            props: {
              status: params.row.enabled == 1 ? "0" : "1"
            }
          },
          params.row.enabled == 1 ? this.$t("common.enable") : this.$t("common.disable")
        );
      }
    },
    {
      title: this.$t("common.operating"),
      align: "left",
      fixed: "right",
      width: 100,
      render: (h, params) => {
        // 是否导入的配置值，系统初始化时通过脚本导入
        let isImport = params.row.source == CONFIG_SOURCE_SYSTEM;
        let data = [];
        if (!isImport && (this.isRIPPLES || this.tenantCode == params.row.tenantCode)) {
          data = [
            {
              type: "icon",
              icon: "md-create",
              text: this.$t("common.edit"),
              hidden: params.row.tenantCode == window.RPConfig.rootTenantCode ? !this.isRIPPLES : false,
              handle: () => {
                this.edit(params.row);
              }
            },
            {
              type: "icon",
              icon: "ios-trash",
              text: this.$t("common.delete"),
              hidden: params.row.enabled == 1 || params.row.scopeLevel == 0,
              handle: () => {
                this.delete(params.row.id);
              }
            }
          ];
        }
        return h("RowTools", {
          props: {
            data
          }
        });
      }
    }
  ];
}
